/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.psi.PsiArrayType;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiType;

/**
 * Created by IntelliJ IDEA.
 * User: rob
 * Date: Oct 5, 2006
 * Time: 7:48:46 AM
 * To change this template use File | Settings | File Templates.
 */
public class ParameterInfo {
    private PsiType psiType;
    private String fieldName;
    private String fieldTypeString;

    public ParameterInfo(String fieldType, String fieldName) {
        this.psiType = null;
        this.fieldTypeString = fieldType;
        this.fieldName = fieldName != null ? fieldName : StringUtil.lcFirst(fieldTypeString);
    }

    public ParameterInfo(PsiType psiType, String fieldName) {
        this.psiType = psiType;
        this.fieldTypeString = psiType.getPresentableText();
        this.fieldName = fieldName != null ? fieldName : StringUtil.lcFirst(fieldTypeString);
    }

    public PsiType getPsiType() {
        return psiType;
    }

    public String getFieldName() {
        return fieldName;
    }

    public String getTestFieldName() {
        if (psiType == null || psiType instanceof PsiPrimitiveType || psiType instanceof PsiArrayType || isNonMockClass()) {
            return fieldName;
        } else {
            return StringUtil.prefix("mock", fieldName);
        }
    }

    public String getFieldTypeString() {
        return fieldTypeString;
    }

    public String toString() {
        return getFieldTypeString() + " " + getFieldName();
    }

    public String getPackageName() {
        if (psiType == null) {
            return null;
        }
        String typeString = psiType.getCanonicalText();
        int endIndex = typeString.lastIndexOf('.');
        return endIndex == -1 ? "" : typeString.substring(0, endIndex);
    }

    private boolean isNonMockClass() {
        String canonicalText = psiType.getCanonicalText();
        return canonicalText.equals("java.lang.String") || canonicalText.equals("java.lang.Class");
    }

}
